-- summon_damage
-- create by panyl
-- 召唤兽伤害技能

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 传入攻击者信息
        local assailantInfo = {
                ["source"] = source;
                ["skill_id"] = skillId;
            };

        -- 伤害怪物
        local ret = { target, };

        -- 技能威力
        local force = para[1];

        -- 攻击方的魔力
        local magic = source:queryAttrib("magic");

        -- 被对方百分比削弱
        local prop = PropM.combine(target, "weak_enemy", "magic");
        magic = PropM.apply(prop, magic);

        -- 被对方绝对值削弱
        prop = PropM.combine(target, "weak_enemy2", "magic");
        magic = PropM.apply(prop, magic);

        -- 3. 伤害/计算伤害时魔力+3
        local damage = math.modf(force * (magic + 3) / 1000);

        -- 增加命中动作
        SkillM.getSequence(target):hit(source, target, skillId);
        -- 伤害
        CombatM.receiveDamage(source, target, damage, skillId, {["assailantInfo"] = assailantInfo});

        -- 溅射
        prop = PropM.combine(source, "extra_target", SkillM.findBaseSkill(skillId));
        local num = PropM.apply(prop, 1);
        -- 溅射伤害比例
        prop = PropM.combine(source, "extra_target_damage", SkillM.findBaseSkill(skillId));
        local rate = PropM.apply(prop, 500);
        local extra_target_damage = math.modf(damage * rate / 1000);
        -- 执行溅射
        if num > 0 then
            for p = 1, GRID_SIZE do
                local grid = DungeonM.getGridByPos(p);
                local monster = grid.monster;

                if num <= 0 then
                    break;
                end

                repeat
                    -- 是未死亡的怪物
                    if  not grid:isOpened() or
                        not monster or
                        grid.monster:isDead() then
                        break;
                    end

                    -- 增加命中动作
                    if SkillM.getSequence(monster) == nil then
                        SkillM.getSequenceList()[monster] = CastSequence.create(source, monster, skillId);
                    end

                    SkillM.getSequence(monster):hit(source, monster, skillId);

                    -- 受创
                    CombatM.receiveDamage(source, monster, extra_target_damage, skillId, {["assailantInfo"] = assailantInfo});

                    num = num - 1;

                    table.insert(ret, grid.monster);
                until true;
            end
        end

        -- 作用目标
        return ret;
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 技能威力
        local force = para[1];

        -- 攻击方的魔力
        local magic = source:queryAttrib("magic");

        -- 伤害/计算伤害时魔力+3
        local damage = math.modf(force * (magic + 3) / 1000);

        desc = string.gsub(desc, "{summon_damage}", damage);

        return desc;
    end,
};
